﻿#region

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

#endregion

namespace Radovici.SharedLibrary
{
    public static class ListFunctionality
    {
        public static bool Equals(IList l1, IList l2)
        {
            if (l1.Count == l2.Count)
            {
                foreach (object item in l1)
                {
                    if (!l2.Contains(item))
                    {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> list)
        {
            var obsCol = new ObservableCollection<T>();
            foreach (T item in list)
            {
                obsCol.Add(item);
            }
            return obsCol;
        }

        public static string Join<T>(this IList<T> l1, Func<T, string> expr)
        {
            return Join(l1, ',', expr);
        }

        public static string Join<T>(this IList<T> l1, char deliminator, Func<T, string> expr)
        {
            string joined = string.Empty;
            foreach (T obj in l1)
            {
                if (!string.IsNullOrEmpty(joined))
                {
                    joined += deliminator;
                }
                joined += expr(obj);
            }
            return joined;
        }

        public static IList<T> Split<T>(this string joined, Func<string, T> expr)
        {
            return Split(joined, ',', expr);
        }

        public static IList<T> Split<T>(this string joined, char deliminator, Func<string, T> expr)
        {
            var split = new List<T>();
            string[] arr = joined.Split(deliminator);
            foreach (string item in arr)
            {
                split.Add(expr(item));
            }
            return split;
        }
    }
}